// packageF/others/customerCenter/component/userDetail/goodsCoupon/goodsCoupon.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    keywork: {
      type: null
    },
    memberId: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    strUrl: 'plugin.customer-center.frontend.controllers.customer.couponsOfMemberByStatusV2',
    getConfig: { client: 'work', type: 17 }, //企业微信公共参数
    couponList: [],
    sidebarList: [],
    json: {
      page: 1,
      coupon_type: 0,
    },
    loading: false,
    finished: false,
  },
  lifetimes: {
    attached() {
      let language = wx.getStorageSync("langIndex");
      this.setData({
        language: language.en,
      });
      this.getData();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getData() {
      if (this.data.finished && this.data.loading) {
        return;
      }
      this.data.loading = true;
      let json = {}
      if (this.data.keywork == 'customer') {
        this.data.strUrl = 'plugin.customer-center.frontend.controllers.customer.couponsOfMemberByStatusV2';
        this.data.json.member_id = this.data.memberId;
        json = { ...this.data.json }
      }
      let urlStr = app.getNetAddresss(this.data.strUrl);
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1 && res.data.data) {
            let listData = res.data.data;
            if (this.data.json.page >= listData.last_page || listData.data.length < listData.per_page) {
              this.setData({
                finished: true
              })
            }
            this.setData({
              couponList: listData.data,
              sidebarList: res.data.search_array,
              'json.page': ++this.data.json.page
            })
            console.log(this.data.couponList)
          } else {
            this.setData({
              finished: true
            })
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    },
    changeCouponType(e) {
      let typeName = e.detail;
      this.setData({
        'json.coupon_type':typeName
      })
      this.getParamInit();
      this.getData();
    },
    loadMore() {
      this.getData();
    },
    getParamInit() {
      this.setData({
        listData: [],
        'json.page': 1,
        loading: false,
        finished: false
      })
    },
  }
})
